package controllers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"testGin/model/mysql"
	"testGin/model/tool"
)

type NavController struct{
	model mysql.Nav
}


func (ctr NavController) Index(c *gin.Context) {
	c.HTML(200, "system/nav/index.html", gin.H{})
}
func (ctr NavController) GetList(c *gin.Context) {

	page := c.DefaultQuery("page", "1")
	limit := c.DefaultQuery("limit", "15")
	where := []map[string]interface{}{}
	pageInt, err := strconv.Atoi(page)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	limitInt, err := strconv.Atoi(limit)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	ctrinId := c.Query("id")
	if ctrinId != "" {
		where = tool.MapWhere("id", "=", ctrinId)
	}
	account := c.DefaultQuery("title", "")
	if account != "" {
		where = tool.MapWhere("title", "=", account)
	}
	result, err := ctr.model.GetList(where, pageInt, limitInt)
	if err != nil {
		tool.Error(c, 1, err.Error())
		return
	}
	tool.SuccessAdmin(c, 0, "请求成功", result["count"].(int), result["data"])
}

func (ctr NavController) Add(c *gin.Context) {

	c.HTML(200, "system/nav/add.html", "")
}

func (ctr NavController) AddAjax(c *gin.Context) {


	if err := c.Bind(&ctr.model); err != nil {
		tool.Error(c, 0, err.Error())
		return
	}
	ctr.model.Status = 1
	errs := ctr.model.Add(ctr.model)
	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}
	tool.Success(c, 200, "添加成功", "")
}

func (ctr NavController) Edit(c *gin.Context) {

	id := c.DefaultQuery("id", "0")

	if ids, _ := strconv.Atoi(id); ids < 0 {
		tool.Error(c, 0, "id不正确")
		return
	} else {
		role, _ := ctr.model.GetItemById(ids)
		c.HTML(200, "system/nav/edit.html", gin.H{
			"item": role,
		})
	}
}

func (ctr NavController) EditAjax(c *gin.Context) {

	if err := c.Bind(&ctr.model); err != nil {
		tool.Error(c, 0, err.Error())
		return
	}

	ctr.model.Status = 1
	errs := ctr.model.Save(ctr.model)

	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}
	tool.Success(c, 200, "修改成功", "")
}
func (ctr NavController) Del(c *gin.Context) {

	id := c.DefaultQuery("id", "0")
	if ids, _ := strconv.Atoi(id); ids < 0 {
		tool.Error(c, 0, "id参数错误")
		return
	} else {

		errs := ctr.model.Del(ids)
		if errs != nil {
			tool.Error(c, 0, errs.Error())
			return
		}
		tool.Success(c, 200, "删除成功", "")
	}

}
func (ctr NavController) Status(c *gin.Context) {
	id := c.DefaultQuery("id", "0")
	status := c.DefaultQuery("status", "0")

	ids, err := strconv.Atoi(id)
	if ids < 0 {
		tool.Error(c, 0, err.Error())
		return
	}

	statuss, err := strconv.Atoi(status)
	if statuss < 0 {
		tool.Error(c, 0, err.Error())
		return
	}

	errs := ctr.model.SetStatus(ids, statuss)

	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}

	tool.Success(c, 200, "操作成功", "")

}
func (ctr NavController) Px(c *gin.Context) {

	id := c.DefaultQuery("id", "0")
	px := c.DefaultQuery("px", "0")

	ids, err := strconv.Atoi(id)
	if ids < 0 {
		tool.Error(c, 0, err.Error())
		return
	}

	pxs, err := strconv.Atoi(px)
	if pxs < 0 {
		tool.Error(c, 0, err.Error())
		return
	}

	errs := ctr.model.Sort(ids, pxs)

	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}

	tool.Success(c, 200, "操作成功", "")
}
